import KBEDebug as log
import KBEngine
import time
import Utils


class AvatarRank(KBEngine.EntityComponent):

    def __init__(self):
        KBEngine.EntityComponent.__init__(self)

    def onAvtarRankEnable(self):
        self.dailyRefresh()

    def dailyRefresh(self):
        if Utils.isCrossMonth(self.seasonRankStartTime):
            self.seasonRankCatchCount = 0
            self.seasonRankStartTime = int(time.time())

    def searchGroup(self, group_id):
        if group_id <= 0:
            self.client.repNotFoundGroup()
        else:
            KBEngine.baseAppData['Lobby'].searchGroup(self.owner, group_id)

    def repSearchGroupErr(self, err_id):
        if err_id == 100:
            self.client.repNotFoundGroup()

    def repSearchGroup(self, creator_id, group_id, group_name, memeber_count, total_catch_count, creatorName, creatorAccount):
        self.client.repSearchGroup(creator_id, group_id, group_name, memeber_count, total_catch_count, creatorName, creatorAccount)

    def reqCreateGroup(self, group_name, group_desc, creatorAccount):
        if len(group_name) == 0:
            self.client.repCreateGroupErr(101)  # 名称为空
            return
        if self.groupId > 0:
            log.ERROR_MSG(f'reqCreateGroup erorr[{self.owner.playerId}]: groupId[{self.groupId}] not zero.')
            self.client.repCreateGroupErr(102)  # 已有村子，不能再次创建
        else:
            KBEngine.baseAppData['Lobby'].createGroup(self.owner, self.owner.playerId, self.owner.nickName, self.owner.headImgUrl, group_name, group_desc, self.seasonRankCatchCount, self.catchTime, creatorAccount)

    def repCreateGroup(self, err_id):
        self.client.repCreateGroupErr(err_id)

    def reqCreateGroupSuccess(self, group_id):
        self.groupId = group_id
        self.joinGroupTime = int(time.time())
        self.client.repCreateGroupErr(0)

    def joinGroup(self, group_id):
        if group_id <= 0:
            self.client.repJoinGroupErr(100)
        else:
            KBEngine.baseAppData['Lobby'].joinGroup(self.owner, group_id, self.owner.playerId, self.seasonRankCatchCount, self.owner.headImgUrl, self.owner.nickName, self.catchTime)

    def joinGroupSuccess(self, group_id):
        self.groupId = group_id
        self.joinGroupTime = int(time.time())
        log.INFO_MSG(f'AvatarRank::joinGroupSuccess[{self.owner.playerId}] {group_id}')
        self.client.onJoinSuccess(group_id)

    def repJoinGroupErr(self, err_id):
        self.client.repJoinGroupErr(err_id)

    def reqExitGroup(self, group_id):
        if group_id <= 0:
            self.client.repExitGroupErr(100)
        else:
            KBEngine.baseAppData['Lobby'].exitGroup(self.owner, self.owner.playerId, group_id, self.seasonRankCatchCount)

    def repExitGroupErr(self, err_id):
        if err_id == 0:  # 退出成功
            self.groupId = 0
        self.client.repExitGroupErr(err_id)

    def updateCatchCount(self):
        self.seasonRankCatchCount += 1
        self.catchTime = int(time.time())
        if self.groupId > 0:
            KBEngine.baseAppData['Lobby'].updateCatchCount(self.owner.playerId, self.groupId, 1)

    def wantExit(self):
        self.client.onWantExit(0)
        return
        # if int(time.time()) - self.joinGroupTime >= 24 * 60 * 60:
        #     self.client.onWantExit(0)
        #     #log.DEBUG_MSG(f'达成退出要求,加入时间:{self.joinGroupTime}')
        # else:
        #     self.client.onWantExit(1)
        #     #log.DEBUG_MSG(f'未达成退出要求,加入时间:{self.joinGroupTime}')

    def searchGroupId(self, group_id):
        KBEngine.baseAppData['Lobby'].searchGroupId(self.owner, group_id)
    
    def repSearchGroupId(self, code, group_id, group_name, member_count, catch_count):
        if code == 0:
            self.client.onSearchGroupErr()
        else:
            self.client.onSearchGroup(group_id, group_name, member_count, catch_count)
    
    def resetGroupId(self):
        self.groupId = 0